//
//  ParticipationTabelViewCell.m
//  Participation
//
//  Created by mac 王少宁 15/3/18.
//  Copyright (c) 2015年 BLUEBLACK. All rights reserved.
//  参与

#import "ParticipationTabelViewCell.h"
#import "ParticipationModel.h"

#define VIEWWIDTH self.mainIgeView.frame.size.width

@implementation ParticipationTabelViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _TopTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, UF(kHeightOfNormalSpace*3), SCREEN_WIDTH, kHeightOfNormalLable*2)];
        _TopTimeLabel.textAlignment = NSTextAlignmentCenter;
        _TopTimeLabel.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
        _TopTimeLabel.font = JKAppGlobleFontSmallTagText;
        [self.contentView addSubview:_TopTimeLabel];
        
        _mainIgeView = CREATE_WITH_FRAME_OF(UIImageView, [self.contentView.ufo ux:24 uy:kHeightOfNormalSpace*0.75 oy:_TopTimeLabel.ufo.yy],
                                            [self.contentView.ubo uw:-48 ow:SCREEN_WIDTH oh:5000]);
        [self.contentView addSubview:_mainIgeView];

        [self addSubviewInMainView];
    }
    return self;
}

- (void)addSubviewInMainView
{
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 0;
    _titleLabel.textColor = [XCommon hexStringToColor:kColor_black_text];
    _titleLabel.font = JKAppGlobleFontMenu;
    [_mainIgeView addSubview:_titleLabel];

    _timeLabel = [[UILabel alloc] init];
    _timeLabel.textColor = [XCommon hexStringToColor:kColor_m_light_gray_text];
    _timeLabel.font = JKAppGlobleFontMinTagText;
    [_mainIgeView addSubview:_timeLabel];

    _myImageView = [[UIImageView alloc] init];
    [_mainIgeView addSubview:_myImageView];
    
    _myTextLabel = [[UILabel alloc] init];
    _myTextLabel.numberOfLines = 3;
    _myTextLabel.textColor = [XCommon hexStringToColor:kColor_gray_text];
    _myTextLabel.font = JKAppGlobleFontSmallTagText;
    [_mainIgeView addSubview:_myTextLabel];
    
    _lineLabel = [[UILabel alloc] init];
    [_lineLabel.layer setMasksToBounds:YES];
    [_lineLabel.layer setBorderWidth:UP(0.5)];
    _lineLabel.layer.borderColor  = [XCommon hexStringToColor:kColor_gray_line].CGColor;
    [_mainIgeView addSubview:_lineLabel];
    
    _readTextLabel = [[UILabel alloc] init];
    _readTextLabel.text = @"阅读全文";
    _readTextLabel.textColor = [XCommon hexStringToColor:kColor_gray_text];
    _readTextLabel.font = JKAppGlobleFontSmallTagText;
    [_mainIgeView addSubview:_readTextLabel];
    
    _nextImage = [[UIImageView alloc] init];
    [_mainIgeView addSubview:_nextImage];
}

-(void)cellWithModel:(ParticipationModel *)model
{    
    _titleLabel.text = model.title;
    CGFloat titleLabelH = [XCommon heightForString:model.title fontSize:15 andWidth:SCREEN_WIDTH - UF(88)] + 1;
    _titleLabel.frame = CGRectOf([_mainIgeView.ufo ux:0 oy:10],
                                 [_mainIgeView.ubo fw:1.0 uw:-40 oh:titleLabelH]);
    
    NSDate * dt = [NSDate dateWithTimeIntervalSince1970:[model.dateline floatValue]];
    NSDateFormatter * formatter1 = [[NSDateFormatter alloc] init];
    formatter1.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDateFormatter * formatter2 = [[NSDateFormatter alloc] init];
    formatter2.dateFormat = @"yyyy-MM-dd";
    NSString * str = [formatter2 stringFromDate:[NSDate date]];
    NSString * str2 = [NSString stringWithFormat:@"%@ 08:00:00",str];
    NSDate * date = [formatter1 dateFromString:str2];
    NSTimeInterval aTimer = [date timeIntervalSinceDate:dt];
    float days = (float)(aTimer/3600/24);
    NSString * timeStr = @"";
    if (days <= 0) {
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"HH:mm";
        NSString * dataStr = [formatter stringFromDate:dt];
        timeStr = [NSString stringWithFormat:@"今天 %@",dataStr];
    }else if (days>0 && days<=1) {
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"HH:mm";
        NSString * dataStr = [formatter stringFromDate:dt];
        timeStr = [NSString stringWithFormat:@"昨天 %@",dataStr];
    } else if (days>1 && days<=2) {
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"HH:mm";
        NSString * dataStr = [formatter stringFromDate:dt];
        timeStr = [NSString stringWithFormat:@"前天 %@",dataStr];
    } else {
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"MM/dd HH:mm";
        timeStr = [formatter stringFromDate:dt];
    }
    _TopTimeLabel.text = timeStr;
    _timeLabel.text = timeStr;
    _timeLabel.frame = CGRectOf([_mainIgeView.ufo fx:0 oy:_titleLabel.ufo.yy+5],
                                [_mainIgeView.ubo fw:1.0 uw:-40 oh:20]);
    
    [_myImageView sd_setImageWithURL:[NSURL URLWithString:model.pic] ];
    
    _myImageView.frame = CGRectMake(UF(20), FYY(_timeLabel)+5, VIEWWIDTH-UF(40), (VIEWWIDTH-UF(40))*0.5);
    _myImageView.backgroundColor = [XCommon hexStringToColor:kColor_s_light_gray_background];
    _myTextLabel.text = model.content;

    _myTextLabel.frame = CGRectOf([_myImageView.ufo fx:0 fy:1.0 oy:15],
                                  [_mainIgeView.ubo fw:1.0 uw:-40 oh:model.contentH]);
    
    _lineLabel.frame = CGRectOf([_mainIgeView.ubo fx:0 oy:_myTextLabel.ufo.yy+15],
                                [_mainIgeView.ubo fw:1.0 ph:1]);
    
    _readTextLabel.frame = CGRectOf([_mainIgeView.ufo fx:0 oy:_lineLabel.ufo.yy+10],
                                    [_mainIgeView.ubo fw:1.0 uw:-40 oh:20]);
    
    _nextImage.frame = CGRectMake(VIEWWIDTH-UF(20)-6, FY(_readTextLabel)+5, 5.6, 10);
    _nextImage.frame = CGRectOf([_mainIgeView.ubo fx:1.0 ux:-20 ox:-6 oy:_readTextLabel.ufo.y+5],
                                [_mainIgeView.ubo ow:5.6 oh:10]);
    _nextImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"arrow_right@2x" ofType:@"png"]];;
    
    _mainIgeView.ufo.height = [self.contentView.ubo oh:_readTextLabel.ufo.yy+10];
    UIImage * cellImg = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"participation_outside@2x" ofType:@"png"]];
    cellImg = [cellImg resizableImageWithCapInsets:UIEdgeInsetsMake(UP(6), UP(6), UP(6), UP(6)) resizingMode:UIImageResizingModeStretch];
    _mainIgeView.image = cellImg;
}

@end
